//
//  PersonalInfoModel.swift
//  NyamanDana
//
//  Created by 李永彬 on 2025/10/30.
//

import SwiftUI

struct PersonalInfoModel: Codable {
    var energyify: [Energyify]
}

struct Energyify: Codable {
    var selenify: String?
    var pitister: String?
    var uvly: String?
    var granally: String?
    var jobfy: String?
    var courseatic: [Courseatic]?
    var plorable: Int?
    var ventrfic: Int?
    var upon: String?
    var onceibility: Bool?
    var fatheraneity: String?
    var sumition: Int?
    var olig: String?
    var eightia: String?
    // 银行不一样字段
    var amicly: String?
    
    enum CodingKeys: CodingKey {
        case selenify
        case pitister
        case uvly
        case granally
        case jobfy
        case courseatic
        case plorable
        case ventrfic
        case upon
        case onceibility
        case fatheraneity
        case sumition
        case olig
        case eightia
        case amicly
    }
    
    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        selenify = try container.decodeIfPresent(String.self, forKey: .selenify)
        pitister = try container.decodeIfPresent(String.self, forKey: .pitister)
        uvly = try container.decodeIfPresent(String.self, forKey: .uvly)
        granally = try container.decodeIfPresent(String.self, forKey: .granally)
        jobfy = try container.decodeIfPresent(String.self, forKey: .jobfy)
        courseatic = try container.decodeIfPresent([Courseatic].self, forKey: .courseatic)
        plorable = try container.decodeIfPresent(Int.self, forKey: .plorable)
        ventrfic = try container.decodeIfPresent(Int.self, forKey: .ventrfic)
        upon = try container.decodeIfPresent(String.self, forKey: .upon)
        onceibility = try container.decodeIfPresent(Bool.self, forKey: .onceibility)
        fatheraneity = try container.decodeIfPresent(String.self, forKey: .fatheraneity)
        sumition = try container.decodeIfPresent(Int.self, forKey: .sumition)
        olig = try container.decodeIfPresent(String.self, forKey: .olig)
        eightia = {
            if let intValue = try? container.decodeIfPresent(Int.self, forKey: .eightia) {
                return String(intValue)
            }
            if let stringValue = try? container.decodeIfPresent(String.self, forKey: .eightia) {
                return stringValue
            }
            return nil
        }()
        amicly = try container.decodeIfPresent(String.self, forKey: .amicly)
    }
}

struct Courseatic: Codable, Identifiable {
    var id = UUID()
    var interestacle: String?
    var olig: String?
    enum CodingKeys: CodingKey {
        case interestacle
        case olig
    }
    
    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        interestacle = try container.decodeIfPresent(String.self, forKey: .interestacle)
        olig = {
            if let intValue = try? container.decodeIfPresent(Int.self, forKey: .olig) {
                return String(intValue)
            }
            if let stringValue = try? container.decodeIfPresent(String.self, forKey: .olig) {
                return stringValue
            }
            return nil
        }()
    }
}
